'use strict'

angular.module('mainApp',[
    'ngAnimate',
    'angular-loading-bar',
    'ui.router',
    'ngDialog',
    'ngFileUpload'
])
.config(['cfpLoadingBarProvider','$stateProvider','$urlRouterProvider','$httpProvider','$locationProvider','ngDialogProvider', function(cfpLoadingBarProvider,$stateProvider, $urlRouterProvider, $httpProvider, $locationProvider, ngDialogProvider) {
    // loading-bar转圈的设置
    // cfpLoadingBarProvider.includeSpinner = false;
    //ngDialog点击空白处弹窗不消失
    ngDialogProvider.setDefaults({
        closeByDocument: false
    });

    // 拦截器设置
    $httpProvider.interceptors.push(tjzHttpInterceptor);

    $locationProvider.hashPrefix('');
    $urlRouterProvider.when("", "/index")
        .when("/user","/user/info")

    $stateProvider
        .state('index', {
            url: '/index',
            templateUrl: 'view/home.html',
            controller: 'indexCtrl'
        })
        .state('items', {
            url: '/items/:type',
            templateUrl: 'view/items.html',
            controller: 'itemsCtrl'
        })
        .state('login', {
            url: '/login',
            templateUrl: 'view/login.html',
            controller: 'loginCtrl'
        })
        .state('user', {
            url: '/user',
            templateUrl: 'view/user/userNav.html',
            controller: 'userCtrl'
        })
        .state('info', {
            url: '/info',
            parent: 'user',
            templateUrl: 'view/user/info.html',
            controller: 'userCtrl'
        })
        .state('order', {
            url: '/order',
            parent: 'user',
            templateUrl: 'view/user/order.html',
            controller: 'orderCtrl'
        })
        .state('favorite', {
            url: '/favorite',
            parent: 'user',
            templateUrl: 'view/user/favorite.html',
            controller: 'favoriteCtrl'
        })
        .state('mycreate', {
            url: '/mycreate',
            parent: 'user',
            templateUrl: 'view/user/mycreate.html',
            controller: 'mycreateCtrl'
        })
        .state('history', {
            url: '/history',
            parent: 'user',
            templateUrl: 'view/user/history.html',
            controller: 'historyCtrl'
        })
        .state('address', {
            url: '/address',
            parent: 'user',
            templateUrl: 'view/user/address.html',
            controller: 'addressCtrl'
        })
        .state('authenticate', {
            url: '/authenticate',
            parent: 'user',
            templateUrl: 'view/user/authenticate.html',
            controller: 'authenticateCtrl'
        })
        .state('person', {
            url: '/person/:userId',
            templateUrl: 'view/personDetail.html',
            controller: 'personDetailCtrl'
        })
        .state('job', {
            url: '/job/:jobId',
            templateUrl: 'view/jobDetail.html',
            controller: 'jobDetailCtrl'
        })
        .state('release', {
            url: '/release',
            templateUrl: 'view/release.html',
            controller: 'releaseCtrl'
        })
        .state('releasePerson', {
            url: '/releasePerson',
            templateUrl: 'view/releasePerson.html',
            controller: 'releasePersonCtrl'
        })
        .state('submitOrder', {
            url: '/submitOrder/:jobId',
            templateUrl: 'view/submitOrder.html',
            controller: 'submitOrderCtrl'
        })
        .state('pay', {
            url: '/pay/:jobId/:id',
            templateUrl: 'view/pay.html',
            controller: 'payCtrl'
        })
        .state('orderDetail', {
            url: '/orderDetail/:jobId/:id',
            templateUrl: 'view/orderDetail.html',
            controller: 'orderDetailCtrl'
        })
        .state('evaluate', {
            url: '/evaluate/:jobId/:id',
            templateUrl: 'view/evaluate.html',
            controller: 'evaluateCtrl'
        })
}])

.constant('constant',{
    "url": "http://127.0.0.1:8080/TJZ"
})
.run(['$rootScope','ngDialog',function($rootScope,ngDialog){
    $rootScope.$on('$locationChangeStart', function (event) {
        ngDialog.closeAll();
    });
}])
.factory('tjzHttpInterceptor', ['$q', tjzHttpInterceptor])


function tjzHttpInterceptor($q) {
    return {
        // 请求发出之前，可以用于添加各种身份验证信息
        request: function(config){
            if(localStorage.token) {
                config.headers.token = localStorage.token;
            }
            return config;
        },
        // 请求发出时出错
        requestError: function(err){
            return $q.reject(err);
        },
        // 成功返回了响应
        response: function(res){
            return res;
        },
        // 返回的响应出错，包括后端返回响应时，设置了非 200 的 http 状态码
        responseError: function(err){
            return $q.reject(err);
        }
    };
}